<template>
  <el-card shadow="hover">
    <!--    查询条件部分-->
    <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">查询条件</span></el-divider>
    <div>
      <el-form ref="showForm" label-position="right" label-width="100px">

        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="出勤信息：">
              <el-input v-model="queryParma.cqxx" clearable size="small" placeholder="请输入查询的出勤信息" />
            </el-form-item>
          </el-col>
          <el-col :span="12" align="right">
            <el-button type="primary" :loading="searchloding" @click="getAttendanceInfoListPageHandle">查询</el-button>
            <el-button @click="resetForm('showForm')">重置</el-button>
            <a style="font-size:14px; margin-left: 10px" @click="toggleSearchStatus = !toggleSearchStatus">
              <template v-if="!toggleSearchStatus">
                展开
                <i class="el-icon-arrow-down" />
              </template>
              <template v-else>
                收起
                <i class="el-icon-arrow-up" />
              </template>
            </a>
          </el-col>
        </el-row>
        <template v-if="toggleSearchStatus">
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="出勤开始时间范围：" label-width="150px">
                <el-date-picker
                  v-model="timeArray.cqkssjArray"
                  clearable
                  type="daterange"
                  align="right"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                  :default-time="['00:00:00', '23:59:59']"
                  @change="setChangeTimeHandle('cqkssj')"
                />
                <span>
                  <i style="margin-left: 5px;font-size: 18px;cursor: pointer" class="el-icon-circle-close" @click="setNoneByType('cqkssj')" />
                </span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="签到记录时间范围：" label-width="150px">
                <el-date-picker
                  v-model="timeArray.qdsjArray"
                  clearable
                  type="daterange"
                  align="right"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions"
                  :default-time="['00:00:00', '23:59:59']"
                  @change="setChangeTimeHandle('qdsj')"
                />
                <span>
                  <i style="margin-left: 5px;font-size: 18px;cursor: pointer" class="el-icon-circle-close" @click="setNoneByType('qdsj')" />
                </span>
              </el-form-item>
            </el-col>
          </el-row>
        </template>
      </el-form>
    </div>
    <!--    查询查询部分-->
    <div>
      <el-divider content-position="left"><span style="font-weight: bolder;font-size: 18px">出勤信息展示</span></el-divider>
      <!--      数据操作按钮-->
      <div style="padding: 10px" align="right">
        <el-button-group>
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
          <el-button type="danger" icon="el-icon-delete" @click="handleDeleteBatch">批量删除</el-button>
        </el-button-group>
      </div>

      <el-table
        :data="dataSource"
        :border="true"
        align="center"
        empty-text="暂无数据"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column
          type="index"
          :index="indexMethod"
          label="#"
          width="50"
          align="center"
        />
        <el-table-column
          label="员工昵称"
          prop="userName"
        />
        <el-table-column
          label="签到时间"
          align="center"
          width="200"
          prop="attendRecodeTime"
        />
        <el-table-column
          label="出勤开始时间"
          align="center"
          width="200"
          prop="attendBeginTime"
        >
          <template slot-scope="scope">
            {{ scope.row.attendBeginTime | formatdate(scope.row.attendBeginTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="出勤结束时间"
          align="center"
          width="200"
          prop="attendEndTime"
        >
          <template slot-scope="scope">
            {{ scope.row.attendEndTime | formatdate(scope.row.attendEndTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注信息"
          prop="memo"
        />
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div align="center">
        <div style="display: inline-block;width: 60px;height:60px;line-height: 60px;text-align: center;font-size: 15px">
          <span style="text-align: center;line-height: 60px;width: 60px;height:60px;">共{{ dataTotals }}条</span>
        </div>
        <div style="display: inline-block">
          <el-pagination
            :current-page.sync="queryParma.pageIndex"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="queryParma.pageSize"
            layout="total, sizes, prev,next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <modal ref="modals" :attendance-info="modifyData" @reflushDataHandle="getAttendanceInfoListPageHandle" />
  </el-card>
</template>

<script>
import { dropBatchAttendanceInfo, getAllAttendanceInfoPage } from '@/api/ht/yytgl/cqgl/cqxxcx'
const { Message } = require('element-ui')
const { fomatMilsToDate } = require('@/utils/DateUtil')
import Modal from '@/views/ht/yytgl/cqgl/cqxxcx/components/modal'
export default {
  name: 'Index',
  components: {
    Modal
  },
  filters: {
    formatdate: function(mils) {
      return fomatMilsToDate(mils)
    }
  },
  data() {
    return {
      text: '正在开发',
      dataSource: [], // 信息列表
      dataTotals: 0, // 查询总条数
      modifyData: {}, // 待修改对象信息
      searchloding: false, // 搜索按钮加载
      toggleSearchStatus: false, // 默认是否展开搜索条件
      queryParma: {
        pageIndex: 1,
        pageSize: 5,
        cqxx: ''
      },
      hasselectedIdArray: [], // 已经选中的信息ID数组
      timeArray: {
        qdsjArray: [],
        cqkssjArray: []
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  mounted() {
    this.getAttendanceInfoListPageHandle() // 获取部门信息
  },
  methods: {
    getAttendanceInfoListPageHandle() {
      this.getAttendanceInfoListPage()
    },
    async getAttendanceInfoListPage() {
      const res = await getAllAttendanceInfoPage(this.queryParma)
      const data = res.data.main.dataArray
      const resp = res.data.respOutMsgHeader
      const totals = res.data.main.totalsData
      const currentPage = res.data.main.currentPage
      if (resp.respCode === 200) {
        setTimeout(() => {
          this.searchloding = false
        }, 500)
        this.dataSource = data
        this.dataTotals = totals
        this.queryParma.pageIndex = currentPage
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    indexMethod(index) {
      return index + 1 + (this.queryParma.pageIndex - 1) * this.queryParma.pageSize
    },
    handleAdd() {
      this.$refs.modals.checkModelType('add')
    },
    handleEdit(index, row) {
      // 使用父子传参方式
      this.modifyData = row
      this.$refs.modals.checkModelType('edit')
    },
    resetForm(formName) {
      // this.$refs[formName].resetFields()
      this.queryParma = {
        pageIndex: 1,
        pageSize: 5,
        cqxx: ''
      }
    },
    handleDelete(index, row) {
      const rowIdarray = []
      rowIdarray.push(row.attendCode)
      this.dropOneAttendanceInfo(rowIdarray)
    },
    handleDeleteBatch() {
      this.dropBatchAttendanceInfo()
    },
    async dropOneAttendanceInfo(array) {
      const res = await dropBatchAttendanceInfo(array)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getAttendanceInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async dropBatchAttendanceInfo() {
      const res = await dropBatchAttendanceInfo(this.hasselectedIdArray)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
        this.getAttendanceInfoListPageHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    handleSizeChange(val) {
      this.queryParma.pageSize = val
      this.getAttendanceInfoListPageHandle()
    },
    handleCurrentChange(val) {
      this.queryParma.pageIndex = val
      this.getAttendanceInfoListPageHandle()
    },
    handleSelectionChange(selections) {
      const rowIdArray = []
      for (const selection of selections) {
        rowIdArray.push(selection.depCode)
      }
      this.hasselectedIdArray = rowIdArray
    },
    setNoneByType(type) {
      this.timeArray = {}
      if (type === 'qdsj') {
        this.queryParma.qdsjks = ''
        this.queryParma.qdsjjs = ''
      } else if (type === 'cqkssj') {
        this.queryParma.cqsjks = ''
        this.queryParma.cqsjjs = ''
      } else {
        this.queryParma.qdsjks = ''
        this.queryParma.qdsjjs = ''
        this.queryParma.cqsjks = ''
        this.queryParma.cqsjjs = ''
      }
    },
    setChangeTimeHandle(str) {
      if (str === 'qdsj') {
        this.queryParma.qjkssjBegin = this.timeArray.qdsjArray[0]
        this.queryParma.qjkssjEnd = this.timeArray.qdsjArray[1]
      } else if (str === 'cqkssj') {
        this.queryParma.cqsjks = this.timeArray.cqkssjArray[0]
        this.queryParma.cqsjjs = this.timeArray.cqkssjArray[1]
      } else {
        console.log('参数出错！')
      }
    }
  }
}
</script>

<style scoped>

</style>
